#!/bin/bash

# Define variables
cpu_num=$(nproc)
worker=$((cpu_num * 2))
ulimit=$(ulimit -HSn)

# Service directory
service_root_dir=$(pwd)
service_root_dir=${service_root_dir%%'/sh'}
service_sh_dir=${service_root_dir}/sh
service_conf_dir=${service_root_dir}/conf
service_vhosts_dir=${service_root_dir}/vhosts
service_www_dir=${service_root_dir}/www
service_samples_dir=${service_root_dir}/samples
service_samples_conf_dir=${service_root_dir}/samples/conf
service_samples_vhosts_dir=${service_root_dir}/samples/vhosts

# Service files
service_env_file=${service_root_dir}/.env
service_docker_compose_file=${service_root_dir}/docker-compose.yml
service_www_index_file=${service_www_dir}/index.php

# All Programs
programs=(
  'nginx'
  'mongo'
  'redis'
  'rabbitmq'
  'mysql(8.0.16)'
  'mysql(latest)'
  'php(5.6)'
  'php(7.3)'
  'php(7.4)'
  'php(8.0)'
)

# Program templates
program_yml_templates=(
  'template_nginx'
  'template_mongo'
  'template_redis'
  'template_rabbitmq'
  # 8.0.16
  'template_mysql'
  # latest
  'template_mysql'
  'template_php56'
  'template_php73'
  'template_php74'
  'template_php80'
)

# Program envs
program_envs=(
  'env_nginx'
  'env_mongo'
  'env_redis'
  'env_rabbitmq'
  # 8.0.16
  'env_mysql_8.0.16'
  # latest
  'env_mysql_latest'
  'env_php56'
  'env_php73'
  'env_php74'
  'env_php80'
)

# All PHP extensions
php_extensions='bcmath,bz2,calendar,curl,exif,ffmpeg,gd,gettext,gifsicle,git,gmp,imagick,imap,inotify,intl,jpegoptim,ldap,mbstring,mcrypt,mongodb,mysql,mysqli,opcache,optipng,pcntl,pdo_mysql,pdo_sqlsrv,phpcbf,phpcs,php-cs-fixer,phpmd,phpstan,pngquant,psalm,pspell,readline,redis,shmop,snmp,soap,sockets,sqlsrv,svgo,swoole,sysvmsg,sysvsem,sysvshm,tidy,wddx,webp,xhprof,xmlrpc,xsl,yaf,zip'

# Helper method
source ${service_sh_dir}/helper.sh
# Templates
source ${service_sh_dir}/templates.sh
# Envs
source ${service_sh_dir}/envs.sh
# Remind
source ${service_sh_dir}/remind.sh

function build_env() {
  first_php=1
  if [ ! -f ${service_env_file} -o ${is_force} == 'y' ]; then
    echo "---------- Building the ${service_env_file} file ----------"
    env_public >${service_env_file}
    install_program_last_index=${install_program_index_arr[@]: -1}
    for i in ${install_program_index_arr[@]}; do
      if [ -n ${program_envs[$((${i} - 1))]} ]; then
        if [ -z "${program_envs[$((${i} - 1))]##*php*}" -a ${first_php} -eq 1 ]; then
          env_php_public >>${service_env_file}
          first_php=0
        fi

        ${program_envs[$((${i} - 1))]} >>${service_env_file}
        if [ ${install_program_last_index} != ${i} ]; then
          echo >>${service_env_file}
        fi
      fi
    done

    sed -i "s;\${ALPINE_VERSION};${alpine_version};g" ${service_env_file}
    if [ -n "${root_dir}" ]; then
      sed -i "s;\${ROOT_DIR_NAME};${root_dir};g" ${service_env_file}
    fi
    if [ "${is_install_mysql}" == 'true' ]; then
      sed -i "s;\${MYSQL_PASSWORD};${mysql_password};g" ${service_env_file}
      sed -i "s;\${MYSQL_PASSWORD};${mysql_password};g" ${service_www_index_file}
    fi
    if [ -n "${install_php_extensions}" -a "${install_php_extensions}" != 'none' ]; then
      sed -i "s;\${PHP_EXTENSIONS};${install_php_extensions};g" ${service_env_file}
    else
      sed -i "s;\${PHP_EXTENSIONS};;g" ${service_env_file}
    fi
  fi
}

function build_docker_compose() {
  if [ ! -f ${service_docker_compose_file} -o ${is_force} == 'y' ]; then
    echo "---------- Building the ${service_docker_compose_file} file ----------"
    template_docker_compose >${service_docker_compose_file}
    install_program_last_index=${install_program_index_arr[@]: -1}
    for i in ${install_program_index_arr[@]}; do
      if [ -n ${program_yml_templates[$((${i} - 1))]} ]; then
        ${program_yml_templates[$((${i} - 1))]} >>${service_docker_compose_file}
        if [ ${install_program_last_index} != ${i} ]; then
          echo >>${service_docker_compose_file}
        fi
      fi
    done
  fi
}

function build_mongod_conf() {
  _samples_file=${service_samples_conf_dir}/mongod.conf
  _target_file=${service_conf_dir}/mongod.conf
  if [ -f ${_samples_file} -a "${is_install_mongo}" == 'true' ]; then
    if [ ! -f ${_target_file} -o ${is_force} == 'y' ]; then
      echo "---------- Copying the ${_target_file} file ----------"
      \cp ${_samples_file} ${_target_file}

      if [ -n "${root_dir}" ]; then
        sed -i "s;\${ROOT_DIR_NAME};${root_dir};g" ${_target_file}
      fi
    fi
  fi
}

function build_mysql_conf() {
  _samples_file=${service_samples_conf_dir}/mysql.cnf
  _target_file=${service_conf_dir}/mysql.cnf
  if [ -f ${_samples_file} -a "${is_install_mysql}" == 'true' ]; then
    if [ ! -f ${_target_file} -o ${is_force} == 'y' ]; then
      echo "---------- Copying the ${_target_file} file ----------"
      \cp ${_samples_file} ${_target_file}
    fi
  fi
}

function build_nginx_conf() {
  _samples_file=${service_samples_conf_dir}/nginx.conf
  _target_file=${service_conf_dir}/nginx.conf
  if [ -f ${_samples_file} -a "${is_install_nginx}" == 'true' ]; then
    if [ ! -f ${_target_file} -o ${is_force} == 'y' ]; then
      echo "---------- Copying the ${_target_file} file ----------"
      \cp ${_samples_file} ${_target_file}

      if [ -n "${root_dir}" ]; then
        sed -i "s;\${ROOT_DIR_NAME};${root_dir};g" ${_target_file}
      fi
      if [ -n "${worker}" ]; then
        sed -i "s;\${WORKER};${worker};g" ${_target_file}
      fi
      if [ -n "${ulimit}" ]; then
        sed -i "s;\${ULIMIT};${ulimit};g" ${_target_file}
      fi
    fi
  fi
}

function build_php_fpm_conf() {
  _samples_file=${service_samples_conf_dir}/php-fpm.conf
  _target_file=${service_conf_dir}/php-fpm.conf
  if [ -f ${_samples_file} -a "${is_install_php}" == 'true' ]; then
    if [ ! -f ${_target_file} -o ${is_force} == 'y' ]; then
      echo "---------- Copying the ${_target_file} file ----------"
      \cp ${_samples_file} ${_target_file}

      sed -i "s;\${CURRENT_USER};$(id -un);g" ${_target_file}
      sed -i "s;\${CURRENT_GROUP};$(id -gn);g" ${_target_file}

      if [ -n "${root_dir}" ]; then
        sed -i "s;\${ROOT_DIR_NAME};${root_dir};g" ${_target_file}
      fi
    fi
  fi
}

function build_php_conf() {
  _samples_file=${service_samples_conf_dir}/php.ini
  _target_file=${service_conf_dir}/php.ini
  if [ -f ${_samples_file} -a "${is_install_php}" == 'true' ]; then
    if [ ! -f ${_target_file} -o ${is_force} == 'y' ]; then
      echo "---------- Copying the ${_target_file} file ----------"
      \cp ${_samples_file} ${_target_file}

      if [ -n "${root_dir}" ]; then
        sed -i "s;\${ROOT_DIR_NAME};${root_dir};g" ${_target_file}
      fi
    fi
  fi
}

function build_rabbitmq_conf() {
  _samples_file=${service_samples_conf_dir}/rabbitmq.config
  _target_file=${service_conf_dir}/rabbitmq.config
  if [ -f ${_samples_file} -a "${is_install_rabbitmq}" == 'true' ]; then
    if [ ! -f ${_target_file} -o ${is_force} == 'y' ]; then
      echo "---------- Copying the ${_target_file} file ----------"
      \cp ${_samples_file} ${_target_file}
    fi
  fi
}

function build_redis_conf() {
  _samples_file=${service_samples_conf_dir}/redis.conf
  _target_file=${service_conf_dir}/redis.conf
  if [ -f ${_samples_file} -a "${is_install_redis}" == 'true' ]; then
    if [ ! -f ${_target_file} -o ${is_force} == 'y' ]; then
      echo "---------- Copying the ${_target_file} file ----------"
      \cp ${_samples_file} ${_target_file}

      if [ -n "${root_dir}" ]; then
        sed -i "s;\${ROOT_DIR_NAME};${root_dir};g" ${_target_file}
      fi
    fi
  fi
}

function build_www_vhosts() {
  _samples_file=${service_samples_vhosts_dir}/www.conf
  _target_file=${service_vhosts_dir}/www.conf
  if [ -f ${_samples_file} -a "${is_install_nginx}" == 'true' ]; then
    if [ ! -f ${_target_file} -o ${is_force} == 'y' ]; then
      echo "---------- Copying the ${_target_file} file ----------"
      \cp ${_samples_file} ${_target_file}

      # php container name
      php80_container_name=$(get_env PHP80_NAME)
      php74_container_name=$(get_env PHP74_NAME)
      php73_container_name=$(get_env PHP73_NAME)
      php56_container_name=$(get_env PHP56_NAME)
      php_container_name=${php80_container_name:-${php74_container_name:-${php73_container_name:-${php56_container_name:-php}}}}
      sed -i "s;\${PHP_CONTAINER_NAME};${php_container_name};g" ${_target_file}

      if [ -n "${root_dir}" ]; then
        sed -i "s;\${ROOT_DIR_NAME};${root_dir};g" ${_target_file}
      fi
    fi
  fi
}

function change_mysql_dir_permissions() {
  mysql_data_dir=$(get_env MYSQL_MACHINE_DATA)
  mysql_data_dir=${mysql_data_dir/'.'/${service_root_dir}}
  if [ -n "${mysql_data_dir}" -a -d "${mysql_data_dir}" -a "${is_install_mysql}" == 'true' ]; then
    echo '---------- Changing mysql folder permissions ----------'
    sudo chown -R 999:999 ${mysql_data_dir} &>/dev/null
    if [ $? -ne 0 ]; then
      echo -e '\033[31mChanging mysql folder permissions fail.\033[0m'
      exit 1
    fi
  fi
}

function install() {
  if [ "${is_install}" == 'y' ]; then
    echo '---------- Installation in progress ----------'
    docker-compose --project-directory ${service_root_dir} up -d
  fi
}

# Perform operations
build_env
build_docker_compose
build_mongod_conf
build_mysql_conf
build_nginx_conf
build_php_fpm_conf
build_php_conf
build_rabbitmq_conf
build_redis_conf
build_www_vhosts
change_mysql_dir_permissions
install

exit 0
